package fm.dongman.animefm.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import fm.dongman.animefm.R;
import fm.dongman.animefm.view.adapter.SearchResultAdapter;
import fm.dongman.animefm.view.util.ActivityHelper;

/**
 * Created by alex on 2017/10/19.
 */

public class SearchResultFragment extends BaseFragment implements View.OnClickListener {
    private static final String ARG_SEARCH_KEYWORDS = "keywords";

    /**
     * @return SearchResultFragment 的实例
     */
    public static SearchResultFragment newInstance(@NonNull String keywords) {
        SearchResultFragment fragment = new SearchResultFragment();
        Bundle args = new Bundle();
        args.putString(ARG_SEARCH_KEYWORDS, keywords);
        fragment.setArguments(args);
        return fragment;
    }

    private TabLayout mTabLayout;
    private EditText mEtSearchInput;
    private ImageButton mIbBack;
    private ImageButton mIbSearchBtn;
    private String mCurKeyWords; // 关键字
    private ViewPager mViewPager;
    private String[] titles = {"动漫", "漫单", "文章", "用户"};

    private BaseFragment[] mBaseFragments;
    private SearchResultAdapter mPagerAdapter; // 搜索结果 Fragment 适配器
    private ArrayList<String> mKeys; // 关键字集合

    public SearchResultFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mCurKeyWords = getArguments().getString(ARG_SEARCH_KEYWORDS);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layoutView = inflater.inflate(R.layout.fragment_search_result, container, false);
        mTabLayout = (TabLayout) layoutView.findViewById(R.id.mTbSearchTab);
        mEtSearchInput = (EditText) layoutView.findViewById(R.id.mActSearchInput);
        mIbBack = (ImageButton) layoutView.findViewById(R.id.mIbSearchBack);
        mIbSearchBtn = (ImageButton) layoutView.findViewById(R.id.mIbSearchBtn);

        mViewPager = (ViewPager) layoutView.findViewById(R.id.mVpSearchResult);

        mBaseFragments = new BaseFragment[4];
        BaseFragment mAnimeFragment = ComicListFragment.Companion.getInstance(ComicListFragment.Companion.getTYPE_SEARCH(), mCurKeyWords);
        BaseFragment mSheetFragment = SheetListFragment.Companion.getInstance(SheetListFragment.Companion.getTYPE_SEARCH(), mCurKeyWords);
        BaseFragment mArticleFragment = ArticleListFragment.Companion.getInstance(ArticleListFragment.Companion.getTYPE_SEARCH(), mCurKeyWords);
        BaseFragment mUserFragment = UserListFragment.Companion.getInstance(UserListFragment.Companion.getTYPE_SEARCH(), mCurKeyWords);

        mBaseFragments[0] = mAnimeFragment;
        mBaseFragments[1] = mSheetFragment;
        mBaseFragments[2] = mArticleFragment;
        mBaseFragments[3] = mUserFragment;

        setKey();
        mPagerAdapter = new SearchResultAdapter(getChildFragmentManager(), titles, mBaseFragments, mKeys);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOffscreenPageLimit(3);

        mTabLayout.setupWithViewPager(mViewPager);

        mIbSearchBtn.setOnClickListener(this);
        mIbBack.setOnClickListener(this);

        mEtSearchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                doSearch();
                hideInputMethod();
                return true;
            }
        });

        mEtSearchInput.setText(mCurKeyWords == null ? "" : mCurKeyWords);


        return layoutView;
    }

    private void setKey() {
        if (mKeys == null){
            mKeys = new ArrayList<>();
        }
        mKeys.clear();
        mKeys.add(mCurKeyWords);
    }

    /**
     * 隐藏输入法
     */
    private void hideInputMethod() {
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(mEtSearchInput.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    @Override
    public void initData() {

    }

    @Override
    public void bindData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mIbSearchBtn:
                doSearch();
                break;
            case R.id.mIbSearchBack:
                ActivityHelper.INSTANCE.backActivity(getActivity());
                break;
        }
    }

    private void doSearch() {
        mCurKeyWords = mEtSearchInput.getText().toString();
        setKey();
        mPagerAdapter.notifyDataSetChanged();
    }
}
